package LinkedList_03.Single_Linked_List_03;

/*
 * @Author 罗
 * @date 2020/3/11 - 4:59 下午
 *  1.链表是以节点的方式来存储，是链式存储
 *  2.每个节点包含data域，next域(指针)，指向下一个节点的地址
 *  3.每一个节点不一定是连续的
 *  4.链表分带 头节点的链表 和 没有头节点的链表，根据实际的需求来确定
 */

import org.junit.Test;

public class test_01 {
    @Test
    public void test_01(){
        SingleLinkedList singleLinked = new SingleLinkedList();
        try {
            singleLinked.addNodeByOrder(new HeroNode(4, "鲁智深", "感觉挺帅"));
            singleLinked.addNodeByOrder(new HeroNode(67, "宋江", "无啦啦"));
            singleLinked.addNodeByOrder(new HeroNode(20, "潘心里安", "好看"));
            singleLinked.addNodeByOrder(new HeroNode(2, "a"));
            singleLinked.addNodeByOrder(new HeroNode(58, "s"));
            singleLinked.addNodeByOrder(new HeroNode(19, "d"));
            singleLinked.addNodeByOrder(new HeroNode(48, "f"));
            singleLinked.addNodeByOrder(new HeroNode(18, "f"));
            singleLinked.addNodeByOrder(new HeroNode(9, "f"));
//            single.addNodeByOrder(new HeroNode(20, "f"));

            singleLinked.modify(new HeroNode(9,"新名字","你是谁"));
            singleLinked.deleteNodeById(2);
            singleLinked.deleteNodeById(67);

        }catch (RuntimeException r){
            System.err.println(r.getMessage());
        }
        singleLinked.showLinkedList();
        System.out.println("有效节点的个数:"+SingleLinkedList.getValidNodeCount(singleLinked.getHead()));

            System.out.println("获取倒数的第几个节点："+SingleLinkedList.getReverseNodeByOrder(singleLinked.getHead(),1));

        /*  不建议使用
            System.out.println("链表反转(我的方法)，反转完之后原链表就坏掉了:");
            SingleLinkedList.reverse(single).showLinkedList();*/

        System.out.println("逆序打印链表（不会破坏链表的结构）");
        SingleLinkedList.reversePrint(singleLinked.getHead());
  /*      System.out.println("链表反转3.0:");
        SingleLinkedList.reverseNew(singleLinked).showLinkedList();*/
        /*课后作业*/
        SingleLinkedList secondLinked = new SingleLinkedList();
        secondLinked.addNodeByOrder(new HeroNode(33, "s"));
        secondLinked.addNodeByOrder(new HeroNode(25, "d"));
        secondLinked.addNodeByOrder(new HeroNode(43, "f"));
        secondLinked.addNodeByOrder(new HeroNode(59, "f"));
        System.out.println("遍历要添加的链表");
        secondLinked.showLinkedList();

        System.out.println("将一个链表添加到当前链表，有序添加");
        singleLinked.addAnotherLinkedList(secondLinked);

        System.out.println("添加完成，遍历");
        singleLinked.showLinkedList();
        System.out.println("添加完成，较小的链表，遍历");
        secondLinked.showLinkedList();
    }
}
